package bean_factory

import (
	. "gitee.com/kristas/booting-go/framework/core/bean"
	"gitee.com/kristas/booting-go/framework/core/log"
)

func init() {
	finishProcessors = append(finishProcessors, new(beanDestroyProcessor))
}

type beanDestroyProcessor struct {
	Log log.Logger `wire:""`
}

func (b *beanDestroyProcessor) Filter(bean Bean) bool {
	return true
}

func (b *beanDestroyProcessor) Do(bean Bean) error {
	beanName := GetBeanName(bean)
	bean.BeanEnd()
	b.Log.Trace(traceLog(bean, "BeanEnd()"))
	if dpb, ok := bean.(DisposableBean); ok {
		err := dpb.Destroy()
		if err != nil {
			b.Log.Error("destroy bean: %s failed, caused by: %s", beanName, err)
		}
		b.Log.Trace(traceLog(bean, "Destroy()"))
	}
	return nil
}

func (b *beanDestroyProcessor) Order() int {
	return orderBeanDestroyProcessor
}
